﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Extentions;
using Microsoft.EntityFrameworkCore;

namespace TrainingReminderSystem.Model
{
    /// <summary>
    /// 店铺
    /// </summary>
    public partial class SysStore
    {
        /// <summary>
        /// 是否是管理员店铺
        /// </summary>
        /// <returns></returns>
        public bool IsAdminStore()
        {
            var r = this.SysStoreID == 1;
            return r;
        }

        /// <summary>
        /// 是否是主店铺
        /// </summary>
        /// <returns></returns>
        public bool IsMainStore()
        {
            var r = this.EnumSysStoreType == EnumSysStoreType.主店铺;
            return r;
        }

        /// <summary>
        /// 得到店铺的主门店ID
        /// </summary>
        /// <returns></returns>
        public int GetParentID()
        {
            if (this.ParentID != null) return this.ParentID.Value;
            return this.SysStoreID;
        }

        /// <summary>
        /// 图文详情
        /// 此字段不映射数据库属性,需要特别注意
        /// </summary>
        [ExLanguageKey("TheContent")]
        [NotMapped]
        public string TheContent { get; set; }

        /// <summary>
        /// TheContent查询sql语句
        /// </summary>
        /// <returns></returns>
        public void TheContentSelectSql(DbContext db)
        {
            string sql =
                $"select {nameof(SysStore.TheContent)} from {nameof(SysStore)} where {nameof(SysStore.SysStoreID)}={this.SysStoreID}";
            object result = db.Database.ExExecuteScalar(sql);
            this.TheContent = result?.ToString();
        }

        /// <summary>
        /// TheContent修改字段语句
        /// </summary>
        /// <param name="db"></param>
        public void TheContentUpdateSql(DbContext db)
        {
            string sql =
                $"update {nameof(SysStore)} set {nameof(SysStore.TheContent)}='{this.TheContent.Replace("'", "''")}' where {nameof(SysStore.SysStoreID)}={this.SysStoreID}";
            db.Database.ExExecuteSqlCommand(sql);
        }
    }
}
